<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class FieldAction extends Action{
    public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
	//产品字段
	/*
	 * alist 从产品字段里查找出来的内容
	 * blist 从导航表里查找出来的导航名字
	 * clist 合并blist里的内容和alist里的内容
	 * list  将clist里的内容赋值给list
	*/
	public function index(){
		$this->checkUser('2_4_1');
		$data = M('Product_field');
		$alist = $data->select();
		$nav = M('Nav');
		foreach($alist as $n=>$val){
			$blist = $nav->where("id = $val[pid]")->field('name')->find();
			if(empty($blist)) $blist['name'] = '<font color=red>顶级分类</font>';
			$clist = array_merge($alist[$n],$blist);
			$list[]=$clist;
		}
		$this->assign('list',$list);
		$this->display();
	}
	public function add(){
		$this->checkUser('2_4_2');
		$data = M('Nav');
		$list = $data->where('type=2')->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($list as $key=>$value){
			$list[$key]['count']=count(explode('-',$value['bpath']));
		}
		$this->assign('list',$list);
		$this->display();
	}
	public function check(){
		$this->checkUser('2_4_2');
		unset($_POST['__hash__']);
		$spec = new Model();
		$field = $_POST['field'];
		@$spec->query("ALTER TABLE `".C('DB_PREFIX')."product_spec` ADD `$field` VARCHAR(100) NOT NULL;");
		$data = M('Product_field');
		$_POST['state'] = '0';
		if(!$data->add($_POST)) $this->error('添加字段失败!请重试!');
		$this->assign("jumpUrl","__URL__/");
		$this->success('添加字段成功!');
	}
	public function edit(){
		$this->checkUser('2_4_2');
		$id = (int)$_GET['id'];
		$data = M('Product_field');
		$list = $data->where("id = $id")->find();
		$this->assign('list',$list);
		$this->display();
	}
	public function amend(){
		$this->checkUser('2_4_2');
		unset($_POST['__hash__']);
		$data = M('Product_field');
		if(!$data->save($_POST)) $this->error('修改字段失败!!');
		$this->assign("jumpUrl","__URL__/");
		$this->success('修改字段成功!');
	}
	public function del(){
		$this->checkUser('2_4_2');
		$spec = new Model();
		$field = $_GET['field'];
		@$spec->query("ALTER TABLE `".C('DB_PREFIX')."product_spec` DROP `$field`;");
		$id = (int)$_GET['id'];
		$data = M('Product_field');
		if(!$data->where("id = $id")->delete())  $this->error('删除字段失败!!');
		$this->success('删除字段成功!');
	}
	public function stop(){
		$this->checkUser('2_4_2');
		$list['id'] = (int)$_GET['id'];
		$list['state'] = (int)$_GET['state'];
		$data = M('Product_field');
		if(!$data->save($list))  $this->error('操作失败!!');
		$this->success('操作成功!');
	}
}
?>